/*
 * Copyright © 2020 www.noark.xyz All Rights Reserved.
 * 
 * 感谢您选择Noark框架，希望我们的努力能为您提供一个简单、易用、稳定的服务器端框架 ！
 * 除非符合Noark许可协议，否则不得使用该文件，您可以下载许可协议文件：
 * 
 * 		http://www.noark.xyz/LICENSE
 *
 * 1.未经许可，任何公司及个人不得以任何方式或理由对本框架进行修改、使用和传播;
 * 2.禁止在本项目或任何子项目的基础上发展任何派生版本、修改版本或第三方版本;
 * 3.无论你对源代码做出任何修改和改进，版权都归Noark研发团队所有，我们保留所有权利;
 * 4.凡侵犯Noark版权等知识产权的，必依法追究其法律责任，特此郑重法律声明！
 */
package com.company.qqsg;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import xyz.noark.core.annotation.Configuration;
import xyz.noark.core.annotation.Value;
import xyz.noark.core.annotation.configuration.Bean;
import xyz.noark.game.NoarkConstant;
import xyz.noark.game.monitor.MonitorManager;
import xyz.noark.game.monitor.impl.GcMonitorService;
import xyz.noark.game.monitor.impl.MemoryMonitorService;
import xyz.noark.game.monitor.impl.NettyDirectMemoryMonitorService;
import xyz.noark.game.monitor.impl.PacketMonitorService;
import xyz.noark.game.monitor.impl.ThreadMonitorService;
import xyz.noark.game.script.GroovyScriptService;
import xyz.noark.game.template.csv.CsvTemplateLoader;
import xyz.noark.orm.accessor.DataAccessor;
import xyz.noark.orm.accessor.sql.mysql.MysqlDataAccessor;
import xyz.noark.orm.write.AsyncWriteService;
import xyz.noark.orm.write.impl.DefaultAsyncWriteServiceImpl;

/**
 * 游戏服务器启动配置类.
 *
 * @author 小流氓(176543888@qq.com)
 */
@Configuration
public class GameServerConfiguration {
	@Value(NoarkConstant.TEMPLATE_PATH)
	private String templatePath;
	@Value("server.zone")
	private String zone = "CN";

	@Value("data.mysql.ip")
	private String mysqlIp;
	@Value("data.mysql.port")
	private int mysqlPort;
	@Value("data.mysql.db")
	private String mysqlDB;
	@Value("data.mysql.user")
	private String mysqlUser;
	@Value("data.mysql.password")
	private String mysqlPassword;

	/**
	 * 策划文件配置
	 */
	@Bean
	public CsvTemplateLoader templateLoader() {
		return new CsvTemplateLoader(templatePath, zone, '	');
	}

	/**
	 * 数据库存档配置
	 */
	@Bean
	public DataAccessor dataAccessor() {
		HikariConfig config = new HikariConfig();
		config.setUsername(mysqlUser);
		config.setPassword(mysqlPassword);
		config.setJdbcUrl(String.format("jdbc:mysql://%s:%d/%s?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false", mysqlIp, mysqlPort, mysqlDB));
		config.addDataSourceProperty("cachePrepStmts", "true"); // 是否自定义配置，为true时下面两个参数才生效
		config.addDataSourceProperty("prepStmtCacheSize", "250"); // 连接池大小默认25，官方推荐250-500
		config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); // 单条语句最大长度默认256，官方推荐2048
		config.addDataSourceProperty("useServerPrepStmts", "true"); // 新版本MySQL支持服务器端准备，开启能够得到显著性能提升
		config.addDataSourceProperty("useLocalSessionState", "true");
		config.addDataSourceProperty("useLocalTransactionState", "true");
		config.addDataSourceProperty("rewriteBatchedStatements", "true");
		config.addDataSourceProperty("cacheResultSetMetadata", "true");
		config.addDataSourceProperty("cacheServerConfiguration", "true");
		config.addDataSourceProperty("elideSetAutoCommits", "true");
		config.addDataSourceProperty("maintainTimeStats", "false");

		MysqlDataAccessor accessor = new MysqlDataAccessor(new HikariDataSource(config));
		accessor.setStatementExecutableSqlLogEnable(false);
		accessor.setStatementParameterSetLogEnable(true);
		accessor.setSlowQuerySqlMillis(1000);// 执行时间超过1秒的都要记录下.
		accessor.setAutoAlterTableDropColumn(true);
		return accessor;
	}

	/**
	 * 数据异步回写服务
	 */
	@Bean
	public AsyncWriteService asyncWriteService() {
		return new DefaultAsyncWriteServiceImpl();
	}

	/**
	 * 动态脚本.
	 */
	@Bean
	public GroovyScriptService scriptService() {
		return new GroovyScriptService();
	}

	/**
	 * 监控管理器.
	 */
	@Bean
	public MonitorManager monitorManager() {
		MonitorManager manager = new MonitorManager();
		manager.addMonitorService(new MemoryMonitorService());
		manager.addMonitorService(new GcMonitorService());
		manager.addMonitorService(new PacketMonitorService());
		manager.addMonitorService(new ThreadMonitorService());
		manager.addMonitorService(new NettyDirectMemoryMonitorService());
		return manager;
	}
}